package cn.hellochaos.generator.service.impl;

import cn.hellochaos.generator.entity.CaipuList;
import cn.hellochaos.generator.entity.User;
import cn.hellochaos.generator.entity.UserCookbook;
import cn.hellochaos.generator.mapper.UserCookbookMapper;
import cn.hellochaos.generator.service.UserCookbookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import cn.hellochaos.generator.exception.bizException.BizException;

import java.util.List;

/**
* <p>
*  服务实现类
* </p>
*
* @author test
* @since 2021-01-23
*/
@Slf4j
@Service
public class UserCookbookServiceImpl extends ServiceImpl<UserCookbookMapper, UserCookbook> implements UserCookbookService {

    @Autowired
    UserCookbookMapper userCookbookMapper;

    /**
     * 如果已经收藏了，就取消，如果没有收藏，就收藏上
     * TODO
     * @param uid
     *        cid
     * @return
     */
    @Override
    public UserCookbook favorite(String uid,String cid) {

        QueryWrapper<UserCookbook> queryWrapper = new QueryWrapper<UserCookbook>();
        queryWrapper.eq("user_id",uid);
        queryWrapper.eq("cookbook_id",cid);

        log.info("用户id：%s,菜谱id：%s",uid,cid);
        List<UserCookbook> userCookbooks = userCookbookMapper.selectList(queryWrapper);
        log.info("用户id：%s,菜谱id：%s,是否收藏?"+userCookbooks,uid,cid);
        if (userCookbooks.size()>0){
            //已经收藏了
            userCookbookMapper.deleteById(userCookbooks.get(0).getUserCookbookId());
        }else{
            UserCookbook userCookbook = new UserCookbook();
            userCookbook.setUserId(Integer.parseInt(uid));
            userCookbook.setCookbookId(Integer.parseInt(cid));

            userCookbookMapper.insert(userCookbook);
        }
        return null;
    }

    /**
     * 判断用户是否收藏了此菜谱
     * @param uid
     * @param cid
     * @return
     */
    @Override
    public Boolean isFavorite(String uid,String cid) {

        QueryWrapper<UserCookbook> queryWrapper = new QueryWrapper<UserCookbook>();
        queryWrapper.eq("user_id",uid);
        queryWrapper.eq("cookbook_id",cid);

        log.info("用户id：%s,菜谱id：%s",uid,cid);
        List<UserCookbook> userCookbooks = userCookbookMapper.selectList(queryWrapper);
        if (userCookbooks.size()<=0){
            // 用户没有收藏此菜谱
            return false;
        }
        return true;
    }

    @Override
    public List<CaipuList> getUserAllFavorite(User user) {

        log.info("user信息:"+user);
        return userCookbookMapper.getUserAllFavorite(user.getUserId());
    }

    /*===================================================*/
    @Override
    public Page<UserCookbook> listUserCookbooksByPage(int page, int pageSize, String factor) {
        log.info("正在执行分页查询userCookbook: page = {} pageSize = {} factor = {}",page,pageSize,factor);
        QueryWrapper<UserCookbook> queryWrapper =  new QueryWrapper<UserCookbook>().like("", factor);
        //TODO 这里需要自定义用于匹配的字段,并把wrapper传入下面的page方法
        Page<UserCookbook> result = super.page(new Page<>(page, pageSize));
        log.info("分页查询userCookbook完毕: 结果数 = {} ",result.getRecords().size());
        return result;
    }

    @Override
    public UserCookbook getUserCookbookById(int id) {
        log.info("正在查询userCookbook中id为{}的数据",id);
        UserCookbook userCookbook = super.getById(id);
        log.info("查询id为{}的userCookbook{}",id,(null == userCookbook?"无结果":"成功"));
        return userCookbook;
    }

    @Override
    public int insertUserCookbook(UserCookbook userCookbook) {
        log.info("正在插入userCookbook");
        if (super.save(userCookbook)) {
            log.info("插入userCookbook成功,id为{}",userCookbook.getUserCookbookId());
            return userCookbook.getUserCookbookId();
        } else {
            log.error("插入userCookbook失败");
            throw new BizException("添加失败");
        }
    }

    @Override
    public int deleteUserCookbookById(int id) {
        log.info("正在删除id为{}的userCookbook",id);
        if (super.removeById(id)) {
            log.info("删除id为{}的userCookbook成功",id);
            return id;
        } else {
            log.error("删除id为{}的userCookbook失败",id);
            throw new BizException("删除失败[id=" + id + "]");
        }
    }

    @Override
    public int updateUserCookbook(UserCookbook userCookbook) {
        log.info("正在更新id为{}的userCookbook",userCookbook.getUserCookbookId());
        if (super.updateById(userCookbook)) {
            log.info("更新d为{}的userCookbook成功",userCookbook.getUserCookbookId());
            return userCookbook.getUserCookbookId();
        } else {
            log.error("更新id为{}的userCookbook失败",userCookbook.getUserCookbookId());
            throw new BizException("更新失败[id=" + userCookbook.getUserCookbookId() + "]");
        }
    }



}
